package com.taiyuan.zfmvp.utils;

import org.springframework.util.DigestUtils;

import java.security.MessageDigest;

/**
 * @param request
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption: md5加密  暂无解密
 * @Date 2022/2/12
 * @return
 */
public class MD5Util {

    /***
     * MD5加密（生成唯一的MD5值）
     * @param data
     * @return
     * @throws Exception
     */
    public static String  md5Encode(String text) throws Exception {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(text.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                String hexString = Integer.toHexString(b & 0xFF);
                if (hexString.length() == 1) {
                    hexString = "0" + hexString;
                }
                sb.append(hexString);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();        }
        return null;
    }

    /**
     * @Author zhoufang@si-tech.com.cn
     * @Description  spring提供的方法
     * @Date 2022/2/12
     * @Param
     * @Return
     */
    public static String md5bySpring(String str){
        return DigestUtils.md5DigestAsHex(str.getBytes());
    }

    public static void main(String[] args) {
        try {
            String str = "sjfX521001&";
            System.out.println(md5bySpring(str));
            System.out.println(md5Encode(str));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
